package com.sky.service.impl;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ConcurrentModificationException;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增购物车
     * 思路：1.传来dto中菜品数据要么为套餐，要么为菜品，因此我先判断如果菜品id不为空，那么执行添加菜品逻辑；否则执行套餐添加逻辑
     * 2.添加套餐/菜品：如果购物车中已经有了要添加的数据，那么则只需要把数据Number+1即可，否则即为第一次添加，就把数据插入进去
     * 3.口味数据，如果有，那就添加，没就不加
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();

        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("当前线程Id为：{}", shoppingCart.getUserId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
//        如果菜品id传过来的话，判断是否为空
        Long dishId = shoppingCartDTO.getDishId();
        if (dishId != null) {
            Dish dish1 = dishMapper.selectId(dishId);
//            不为空，判断是否已经有
            List<ShoppingCart> dish = shoppingCartMapper.findByCondition(shoppingCart);
            if (dish != null && dish.size() == 1) {
                log.info("dish的大小或长度为{}", dish.size());

//                //                    得到单价
//                BigDecimal priceSingle = dish1.getPrice();

////                 得到现在的价钱
//                BigDecimal bigDecimal = priceSingle.multiply(new BigDecimal(shoppingCart.getNumber()));
////                 更新价钱
//                shoppingCart.setAmount(bigDecimal);
//                并把数据修改到表中
                //                  更改当前数量
                shoppingCart.setNumber(dish.get(0).getNumber() + 1);
//               将id设置为当前购物车的主键id
                shoppingCart.setId(dish.get(0).getId());
//                设置口味
//                先得到传来的的口味数据
                String dishFlavor = shoppingCartDTO.getDishFlavor();
//                得到之前的口味
                String dishFlavorBefore = dish.get(0).getDishFlavor();
//                如果现在的口味和之前的口味不一样，且有数据，那就添加并指定第几份
                if (dishFlavor != null && dishFlavor != "" && (!dishFlavor.equals(dishFlavorBefore))) {
                    shoppingCart.setDishFlavor(dishFlavorBefore + ";第" + shoppingCart.getNumber() + "份是" + dishFlavor + "口味");
                    shoppingCartMapper.update(shoppingCart);
                    return;
                } else {
                    //更新数据
                    shoppingCartMapper.update(shoppingCart);
                    return;
                }
            }
//从dish表中查出name，根据Id
//        没有那就设置数量为1,并且插入购物车表中
            shoppingCart.setNumber(1);
            shoppingCart.setAmount(dish1.getPrice());
            shoppingCart.setImage(dish1.getImage());
            shoppingCart.setName(dish1.getName());
        } else {
            List<ShoppingCart> setmeal = shoppingCartMapper.findByCondition(shoppingCart);
            log.info("setmeal的size为{}", setmeal.size());
            Setmeal setmealById = setmealMapper.findSetmealById((shoppingCartDTO.getSetmealId()).intValue());
            if (setmeal != null && setmeal.size() == 1) {

//                  更改当前数量
                shoppingCart.setNumber(setmeal.get(0).getNumber() + 1);
//                设置id为之前的id
                shoppingCart.setId(setmeal.get(0).getId());
                shoppingCartMapper.update(shoppingCart);
                return;
            } else {
//            第一次添加   //            查找套餐表得到名字价格等
                shoppingCart.setNumber(1);
                shoppingCart.setImage(setmealById.getImage());
                shoppingCart.setAmount(setmealById.getPrice());
                shoppingCart.setName(setmealById.getName());
            }
        }

//走到这就意味着为第一次操作，执行插入操作
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 查看购物车数据
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart cart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> listShoppingCart = shoppingCartMapper.findByCondition(cart);
        return listShoppingCart;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        ShoppingCart cart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        shoppingCartMapper.delete(cart);
    }

    /**
     * 删除单个数据
     * @param shoppingCartDTO
     */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.findByCondition(shoppingCart);
        shoppingCart = list.get(0);
//        判断该用户的购物车里是否还有数据
        if (list.size() == 1) {
//            得到该对象的数量
            Integer number = shoppingCart.getNumber();
            if (number > 1) {
//                该套餐或者菜品数量大于1,那就数量-1
                shoppingCart.setNumber(number - 1);
                shoppingCartMapper.update(shoppingCart);
            } else {
//                删除之前一定要记得指定userid
//                该套餐或者菜品数量为1，那就删除
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }
        }
//        list中无数据，也就来不到这里

    }
}
